package com.zy.crm.workbench.controller;

import com.zy.crm.exception.LoginFailException;
import com.zy.crm.workbench.domain.Manage;
import com.zy.crm.workbench.service.ManageService;
import com.zy.crm.vo.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author CYH

 */

@Controller   // controller层只负责调用业务层处理逻辑  和返回结果
public class ManageController {

    // 引用类型自动注入 由于springMVC容器是spring的子容器 所以可以从spring容器中拿出userService
    @Resource
    private ManageService manageService;

    // 验证用户登录的方法
    @RequestMapping("/workbench/manage/login.do" )
    @ResponseBody
    public Object ManageLogin(String loginAct, String loginPwd, HttpServletRequest request) throws LoginFailException {

        // 创建返回消息的map
        Message msg = new Message();
        //System.out.println("进入login");
        // 拿到访问主机的ip地址
        String ip = request.getLocalAddr();
        //System.out.println(ip);

        Manage manage = manageService.queryManageLogin(loginAct,loginPwd,ip);

        // 如果service层代码没有抛出异常的话 将用户对象放入会话作用域对象 并完成提交
        //  抛出了异常会被exceptionHandler捕捉 帮助完成异常信息的回传
        request.getSession().setAttribute("manage", manage);

        msg.setSuccess(true);
        return msg;
    }


//修改密码
@RequestMapping("/workbench/manage/UpdateUser.do")
@ResponseBody
public Object ManageUpDate(String loginAct, String loginPwd,String newloginPwd,String newloginPwd2, HttpServletRequest request) {

    // 创建返回消息的map
    Message msg = new Message();

//    System.out.println("账号"+loginAct);
//    System.out.println("密码"+loginPwd);
//    System.out.println("新密码"+newloginPwd);
//    System.out.println("新密码"+newloginPwd2);
    int num = manageService.updatePassword(loginAct,loginPwd,newloginPwd,newloginPwd2);
//    System.out.println("控制层的"+num);
    // 如果service层代码没有抛出异常的话 将用户对象放入会话作用域对象 并完成提交
    //  抛出了异常会被exceptionHandler捕捉 帮助完成异常信息的回传
    // request.getSession().setAttribute("user",user);
    if(num>0){
        msg.setSuccess(true);
    }

    return msg;
    }



}
